package org.come.Jpanel;

import java.awt.Dimension;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

import org.come.Frame.ContinuousRechargeJframe;
import org.come.bean.ChongjipackBean;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.CutButtonImage;
import org.come.until.FormsManagement;
import org.come.until.SrcollPanelUI;

import com.tool.btn.FormsOnOffBtn;
import com.tool.role.RoleData;

/**
 * HGC-2019-11-21 连续充值
 * 
 * @author Administrator
 * 
 */
public class ContinuousRechargeJpanel extends JPanel {

	/**
	 * 物品List
	 */
	private List<ContinuousRechargeGoodsJpanel> goodsJpanelList;
	private JScrollPane scrollPane;

	private List<ChongjipackBean> chongjipackBeans;

	public ContinuousRechargeJpanel() {
		setPreferredSize(new Dimension(682, 479));
		setOpaque(false);
		setLayout(null);

		// 关闭按钮事件
		FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/hongmu/1/s74.png", 1, 101);
		offBtn.setBounds(657, 0, 25, 25);
		this.add(offBtn);

		getScrollPane();
		getGoodsJpanelList();

	}

	/** 获取连续充值列表 */
	public void getGoods() {
		String sendmes = Agreement.getAgreement().DayforweekgradegetAgreement();
		SendMessageUntil.toServer(sendmes);
	}

	/**
	 * 添加数据
	 * 
	 * @param chongjipackBeans
	 */
	public void showGoods(List<ChongjipackBean> chongjipackBeans) {
		if (chongjipackBeans == null) {
			chongjipackBeans = new ArrayList<>();
		}
		this.chongjipackBeans = chongjipackBeans;
		String vipget = RoleData.getRoleData().getLoginResult().getVipget();
		String[] split = null;
		if (vipget != null) {
			split = vipget.split("&&");
		}
		for (int i = 0; i < goodsJpanelList.size(); i++) {
			if (i < chongjipackBeans.size()) {
				if (goodsJpanelList.size() < chongjipackBeans.size()) {
					goodsJpanelList.add(new ContinuousRechargeGoodsJpanel());
				}
				if (ImpactGradeGoodsJpanel.checkYesOrNo(split, "3", chongjipackBeans.get(i).getPackgradetype() + "")) {
					goodsJpanelList.get(i).getGetBtn().setBtn(-1);
					goodsJpanelList.get(i).getGetBtn().setIcon(CutButtonImage.getImage("inkImg/hongmu/1/yilingqu.png", -1, -1));
				} else {
					goodsJpanelList.get(i).getGetBtn().setBtn(1);
					try {
						goodsJpanelList.get(i).getGetBtn().setIcons(CutButtonImage.cuts("inkImg/hongmu/1/lingqu.png"));
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				goodsJpanelList.get(i).showGoods(chongjipackBeans.get(i));
			} else {
				goodsJpanelList.get(i).showGoods(null);
			}
		}
		addGoods();
	}

	/**
	 * 刷新面板
	 */
	public void refreshPanel() {
		showGoods(chongjipackBeans);
	}

	/**
	 * 展示滚动条里面的列表
	 */
	public void addGoods() {
		JPanel view = (JPanel) scrollPane.getViewport().getView();
		view.removeAll();
		view.repaint();
		int num = 0;
		for (int i = 0; i < goodsJpanelList.size(); i++) {
			ContinuousRechargeGoodsJpanel vipGoodsJpanel = goodsJpanelList.get(i);
			if (vipGoodsJpanel.getChongjipackBean() == null) {
				continue;
			}
			vipGoodsJpanel.setBounds(10, num * 62, 562, 60);
			num++;
			view.add(vipGoodsJpanel);
		}
		view.setPreferredSize(new Dimension(590, 62 * num));
		scrollPane.updateUI();
		scrollPane.invalidate();
		scrollPane.validate();
		scrollPane.repaint();

		if (!ContinuousRechargeJframe.getContinuousRechargeJframe().isVisible()) {
			FormsManagement.showForm(101);
		}
	}

	private ImageIcon iconBack;
	private ImageIcon iconTop;

	@Override
	protected void paintComponent(Graphics g) {
		// TODO Auto-generated method stub
		super.paintComponent(g);

		if (iconBack == null) {
			iconBack = CutButtonImage.getImage("inkImg/old/background/S9.png", -1, -1);
		}
		// 底板_w682,h404
		g.drawImage(iconBack.getImage(), 0, 0, 682, 479, this);

		//if (iconTop == null) {
			//iconTop = CutButtonImage.getImage("inkImg/old/background/S4.png", -1, -1);
		//}
		//g.drawImage(iconTop.getImage(), 55, 23, 591, 116, this);
	}

	public List<ContinuousRechargeGoodsJpanel> getGoodsJpanelList() {
		if (goodsJpanelList == null) {
			goodsJpanelList = new ArrayList<>();
			for (int i = 0; i < 10; i++) {
				goodsJpanelList.add(new ContinuousRechargeGoodsJpanel());
			}
		}
		return goodsJpanelList;
	}

	public void setGoodsJpanelList(List<ContinuousRechargeGoodsJpanel> goodsJpanelList) {
		this.goodsJpanelList = goodsJpanelList;
	}

	public JScrollPane getScrollPane() {
		if (scrollPane == null) {
			// 属性滚动条
			scrollPane = new JScrollPane();
			scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
			scrollPane.getVerticalScrollBar().setUI(new SrcollPanelUI());
			scrollPane.getVerticalScrollBar().setUnitIncrement(20);
			scrollPane.getViewport().setOpaque(false);
			scrollPane.setOpaque(false);
			scrollPane.setBounds(55, 176, 590, 281);
			scrollPane.setBorder(BorderFactory.createEmptyBorder());
			scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
			JPanel jPanel = new JPanel();
			jPanel.setBackground(null);
			jPanel.setOpaque(false);
			jPanel.setBorder(null);
			jPanel.setLayout(null);
			scrollPane.setViewportView(jPanel);
			this.add(scrollPane);

		}
		return scrollPane;
	}

	public void setScrollPane(JScrollPane scrollPane) {
		this.scrollPane = scrollPane;
	}

	public ImageIcon getIconBack() {
		return iconBack;
	}

	public void setIconBack(ImageIcon iconBack) {
		this.iconBack = iconBack;
	}

	public ImageIcon getIconTop() {
		return iconTop;
	}

	public void setIconTop(ImageIcon iconTop) {
		this.iconTop = iconTop;
	}

}
